package pms.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Set;


import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.om.BewDat;
import pms.om.Image;
import pms.om.Person;
import pms.om.Raum;

/**
 * @author josef@dr-schneeberger.de
 */
public class PersonDao extends HibernateDaoSupport {
	/**
	 * returns a single person by its primary db key
	 * @param id
	 * @return the Person
	 */
	public Person findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Person) template.get(Person.class, id);
	}
	
	/**
	 * @return all Person objects from the database.
	 */
	@SuppressWarnings("unchecked")
	public List<Person> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		List results = template.loadAll(Person.class);
		return results;
	}

	/**
	 * creates and persists a new Person object.
	 * @param vorname
	 * @param nachname
	 * @param telefon
	 * @param abteilung
	 */
	public void create(String vorname, String nachname, String telefon, String abteilung, Date ts, int pr, int hours, byte[] pic1,  byte[] pic2, 
			 byte[] pic3, byte[] pic4, byte[] pic5, byte[] pic6, byte[] pic7, byte[] pic8, byte[] pic9,
			 byte[] pic10, byte[] pic11, byte[] pic12, byte[] pic13, byte[] pic14, byte[] pic15, byte[] pic16) {
//		System.out.print("gefunden H ");//hbi
//		String ImagePath = "c:\\workspace\\hbi\\WebContent\\images\\pon";
//		File file1 = new File(ImagePath + "/1.jpg");
//    	byte[] bFile1 = new byte[(int) file1.length()];
//        try {
//            //convert file into array of bytes
//   	     FileInputStream fileInputStream1 = new FileInputStream(file1); 
//   	     fileInputStream1.read(bFile1); 
//   	     fileInputStream1.close();}
//	     catch (Exception e) {
//		     e.printStackTrace();
//	    }
	    HibernateTemplate template = getHibernateTemplate(); 
	    Person p = new Person(vorname, nachname, telefon, abteilung, ts, pr, hours ,  pic1, pic2, pic3, pic4, pic5, pic6, pic7,
				pic8, pic9, pic10, pic11, pic12, pic13, pic14, pic15, pic16);
		template.persist(p);
	}

	/**
	 * Modifies an existing persistent Person object.
	 * @param id the primary key used in the database
	 * @param vorname
	 * @param nachname
	 * @param telefon
	 * @param abteilung
	 */
	public void modify(int id, String vorname, String nachname, String telefon, String abteilung,Date ts, int pr,  int hr) {
		HibernateTemplate template = getHibernateTemplate();
		Person p = (Person) template.get(Person.class, id);
		p.setVorname(vorname);
		p.setNachname(nachname);
		p.setTelefon(telefon);
		p.setRaum(abteilung);
		p.setPresent(pr);
		p.setTimeStamp(ts);
		p.setHours(hr);
		template.saveOrUpdate(p);
		System.out.print("gefunden B ");//hbi
	}

	public void modify(Person p) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(p);
	}
	
	/**
	 * deletes a person using it's id.
	 * @param id
	 * @throws PersonDeleteException if the Person is stored in a Bewdat object
	 */
	public void deleteById(int id) throws PersonDeleteException {
		HibernateTemplate template = getHibernateTemplate();
		Person p = findById(id);
//		hier ist ein Fehler
		Set<BewDat> bewdaten = p.getBewdaten();
		if (bewdaten == null ) {
			template.delete(p);
		}else {
			template.delete(p) ;
		}
		template.flush();
	}
	public int countIDs(){
		return findAll().size() ;
	}

	public void writeImgFiles() {
	String ImagePath ="C:\\workspace\\hbi\\WebContent\\images\\dbImages\\" ;
	List<Person> personen = findAll() ;
	for (Person person : personen) {
//	    person = person.getPersonDao().findById(idDs);
	    byte[] bImage1 = person.getPicture1();
	    byte[] bImage2 = person.getPicture2();
	    byte[] bImage3 = person.getPicture3();
	    byte[] bImage4 = person.getPicture4();
	    byte[] bImage5 = person.getPicture5();
	    byte[] bImage6 = person.getPicture6();
	    byte[] bImage7 = person.getPicture7();
	    byte[] bImage8 = person.getPicture8();
	    byte[] bImage9 = person.getPicture9();
	    byte[] bImage10 = person.getPicture10();
	    byte[] bImage11 = person.getPicture11();
	    byte[] bImage12 = person.getPicture12();
	    byte[] bImage13 = person.getPicture13();
	    byte[] bImage14 = person.getPicture14();
	    byte[] bImage15 = person.getPicture15();
	    byte[] bImage16 = person.getPicture16();

        try{
			  FileOutputStream fos1 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 0 + ".jpg"); fos1.write(bImage1); fos1.close();
			  FileOutputStream fos2 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 1 + ".jpg"); fos2.write(bImage2); fos2.close();
			  FileOutputStream fos3 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 2 + ".jpg"); fos3.write(bImage3); fos3.close();
			  FileOutputStream fos4 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 3 + ".jpg"); fos4.write(bImage4); fos4.close();
			  FileOutputStream fos5 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 4 + ".jpg"); fos5.write(bImage5); fos5.close();
			  FileOutputStream fos6 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 5 + ".jpg"); fos6.write(bImage6); fos6.close();
			  FileOutputStream fos7 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 6 + ".jpg"); fos7.write(bImage7); fos7.close();
			  FileOutputStream fos8 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 7 + ".jpg"); fos8.write(bImage8); fos8.close();
			  FileOutputStream fos9 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 8 + ".jpg"); fos9.write(bImage9); fos9.close();
			  FileOutputStream fos10 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 9 + ".jpg"); fos10.write(bImage10); fos10.close();
			  FileOutputStream fos11 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 10 + ".jpg"); fos11.write(bImage11); fos11.close();
			  FileOutputStream fos12 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 11 + ".jpg"); fos12.write(bImage12); fos12.close();
			  FileOutputStream fos13 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 12 + ".jpg"); fos13.write(bImage13); fos13.close();
			  FileOutputStream fos14 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 13 + ".jpg"); fos14.write(bImage14); fos14.close();
			  FileOutputStream fos15 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 14 + ".jpg"); fos15.write(bImage15); fos15.close();
			  FileOutputStream fos16 = new FileOutputStream(ImagePath + "AllDbImage\\" + person.getId() + "_" + 15 + ".jpg"); fos16.write(bImage16); fos16.close();
			}catch (Exception e){}
		}
	}
}
